<template>
  <div class="loading-container">
    <div class="loading-spinner"></div>
    <p>正在跳转到管理后台...</p>
  </div>
</template>

<script setup>
import { onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useAdminStore } from '@/stores/admin'

const router = useRouter()
const adminStore = useAdminStore()

onMounted(() => {
  // 从URL获取参数
  const urlParams = new URLSearchParams(window.location.search)
  const encodedData = urlParams.get('data')
  
  if (encodedData) {
    try {
      // 解码数据
      const { token, userInfo } = JSON.parse(decodeURIComponent(encodedData))
      console.log('接收到的用户信息:', userInfo)
      
      // 存储到Pinia和localStorage
      adminStore.setToken(token)
      adminStore.setUser(userInfo)
      localStorage.setItem('adminToken', token)
      localStorage.setItem('adminUserInfo', JSON.stringify(userInfo))
      
      // 清除URL中的敏感数据
      window.history.replaceState({}, '', '/dashboard')
      
      // 跳转到管理后台
      router.push('/dashboard')
    } catch (error) {
      console.error('数据解析失败:', error)
      router.push('/login?error=invalid_data')
    }
  } else {
    router.push('/login')
  }
})
</script>

<style scoped>
.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
}
.loading-spinner {
  border: 4px solid #f3f3f3;
  border-top: 4px solid #3b82f6;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}
@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
</style>